﻿using System;
using System.Windows.Forms;
using TestCenter.Classes;

namespace TestCenter.Forms
{
    public partial class FormUser : Form
    {
        public FormUser()
        {
            InitializeComponent();
        }

        private void FormUserLoad(object sender, EventArgs e)
        {
            UpdateUserInfo();

            cbbSoftware.SelectedIndex = 0;
            cbbNumberOfQuestion.SelectedIndex = 4;
            cbbTime.SelectedIndex = 3;
        }

        private void UpdateUserInfo()
        {
            if (Global.CurrentUser == null) return;

            var user = Global.CurrentUser;
            Text = "User: " + user.Username;
            lbUsername.Text = user.Username;
            lbFullName.Text = user.FullName;
            lbSex.Text = user.Sex;
            lbEmail.Text = user.Email;
            lbDateOfBirth.Text = user.DateOfBirth.ToLongDateString();
        }

        private void FormUserFormClosed(object sender, FormClosedEventArgs e)
        {
            if (Global.FrmLogin != null)
            {
                Global.FrmLogin.Show();
                Global.FrmLogin.BringToFront();
            }
        }

        private void BtnStartPracticeClick(object sender, EventArgs e)
        {
            var test = new Test
            {
                Name = "Test",
                Software = cbbSoftware.Text,
                TotalQuestion = int.Parse(cbbNumberOfQuestion.Text),
                TimeInSecond = int.Parse(cbbTime.Text) * 60
            };

            var s = "\tThông tin bài thi:";
            s += "\nPhần mềm:\t" + test.Software;
            s += "\nSố câu hỏi:\t" + test.TotalQuestion + " câu";
            s += "\nThời gian làm bài:\t" + test.TimeInSecond + " giây";
            s += "\n\nBạn có muốn thực hiện bài thi này?";

            var dr = MessageBox.Show(s, "Xác nhận",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
                return;

            DoStartPractice(test);
        }

        private void DoStartPractice(Test test)
        {
            Global.CurrentTestManager = new TestManager(test);

            if (Global.FrmTest == null || Global.FrmTest.IsDisposed)
                Global.FrmTest = new FormTest();

            Visible = false;
            Global.FrmTest.Show();
        }
    }
}
